package com.hyc.myvideo.ui.activity.home;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.acmenxd.toaster.Toaster;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.CurriculumHourAdapter;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.Curriculum;
import com.hyc.myvideo.data.api.model.CurriculumData;
import com.hyc.myvideo.data.api.model.CurriculumHour;
import com.hyc.myvideo.ui.activity.my.LoginActivity;
import com.hyc.myvideo.ui.activity.my.RechargeActivity;
import com.hyc.myvideo.ui.base.BaseFActivity;
import com.hyc.myvideo.utils.imguitl.ImgUitl;
import com.hyc.myvideo.widget.VIPsDialog;
import com.hyc.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 系列列表
 * Created by dfeads on 2017/7/21.
 */

public class TrainingCatalogActivity extends BaseFActivity {
  /** 顶部背景图片 */
  @BindView(R.id.activity_training_catalog_top_img) ImageView mTopImg;
  /** 开通会员 */
  @BindView(R.id.activity_training_catalog_bt_member) Button mBtMember;
  /** 开通会员模块 */
  @BindView(R.id.activity_training_catalog_rl_member) RelativeLayout mRlMember;
  /** 课程数量 */
  @BindView(R.id.activity_training_catalog_tv_num) TextView mTvNum;
  /** 添加到 */
  @BindView(R.id.activity_training_catalog_bt_add) Button mBtAdd;
  /** 开通VIP(购买课程) */
  @BindView(R.id.activity_training_catalog_bt_vip) Button mBtVip;
  /** 开通VIP(购买课程)模块 */
  @BindView(R.id.activity_training_catalog_rl_bt) LinearLayout mRlBt;

  @BindView(R.id.activity_training_catalog_list_view) PullToRefreshListView mListView;
  private String curriculum_id = "";

  private CurriculumHourAdapter curriculumHourAdapter;
  private List<CurriculumHour> curriculumHourList;
  private CurriculumData curriculumData;
  private VIPsDialog vipsDialog;

  @Override public int getContentViewId() {
    return R.layout.activity_training_catalog;
  }

  @Override protected void initView() {
    setLeftBtnClick(true, "修炼");
    setRightClick(R.mipmap.audio_share, new View.OnClickListener() {
      @Override public void onClick(View view) {

      }
    });

    vipsDialog = new VIPsDialog(this);
    vipsDialog.setOnVIPsListener(new VIPsDialog.OnVIPsListener() {
      @Override public void toSure(int type) {
        Toaster.show("123");
      }
    });

    curriculum_id = getIntent().getStringExtra("id");

    curriculumHourList = new ArrayList<>();
    curriculumHourAdapter = new CurriculumHourAdapter(this, curriculumHourList);
    mListView.setAdapter(curriculumHourAdapter);

    if (!StringUtils.isEmpty(curriculum_id)) {
      api.getHomeClassHourList(curriculum_id);
    }
    mListView.setMode(PullToRefreshBase.Mode.DISABLED);
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        if (curriculumData == null || curriculumHourList.isEmpty()) {
          return;
        }
        CurriculumHour curriculumHour = curriculumHourList.get(position - 1);
        if (userInfo.isLogin()) {
          //user_vip_state 0=普通会员,1=vip会员,2=永久vip会员
          //curriculum_charge_state  0=免费，1=vip,2=收费
          switch (userInfo.getUser_vip_state()) {
            case 0:
              switch (curriculumData.getCurriculum_charge_state()) {
                case 0:
                  setAct(curriculumHour);
                  break;
                case 1:
                  vipsDialog.show();
                  break;
                case 2:// 0未支付,1已经支付
                  if (curriculumData.getOrder_state() == 0) {
                    startActivity(new Intent(TrainingCatalogActivity.this,
                        ConfirmPurchaseActivity.class).putExtra("id",
                        curriculumHour.getCurriculum_id()).putExtra("type", 3));
                  } else {
                    setAct(curriculumHour);
                  }
                  break;
              }
              break;
            case 1:
              //收费课程处理
              if (curriculumData.getCurriculum_charge_state() == 2) {
                if (curriculumData.getOrder_state() == 0) {
                  startActivity(new Intent(TrainingCatalogActivity.this,
                      ConfirmPurchaseActivity.class).putExtra("id",
                      curriculumHour.getCurriculum_id()).putExtra("type", 3));
                } else {
                  setAct(curriculumHour);
                }
              } else {
                setAct(curriculumHour);
              }
              break;
            case 2:
              setAct(curriculumHour);
              break;
          }
        } else {
          //未登录 免费课程 进入,其他进登录
          //curriculum_charge_state  0=免费，1=vip,2=收费
          switch (curriculumData.getCurriculum_charge_state()) {
            case 0:
              setAct(curriculumHour);
              break;
            default:
              startActivity(new Intent(TrainingCatalogActivity.this, LoginActivity.class));
              break;
          }
        }
        setAct(curriculumHour);
      }
    });
  }

  /**
   * list跳转
   */
  private void setAct(CurriculumHour curriculumHour) {
    //1=视频,2=音频,3=图文
    switch (curriculumHour.getCurriculum_hour_type()) {
      case 1:
        startActivity(new Intent(TrainingCatalogActivity.this, VideoActivity.class).putExtra("id",
            curriculumHour.getCurriculum_hour_id()));
        break;
      case 2:
        startActivity(new Intent(TrainingCatalogActivity.this, AudioActivity.class).putExtra("id",
            curriculumHour.getCurriculum_hour_id()));
        break;
      case 3:
        startActivity(
            new Intent(TrainingCatalogActivity.this, HomeDetailActivity.class).putExtra("id",
                curriculumHour.getCurriculum_hour_id()));
        break;
    }
  }

  @Override protected void initData() {

  }

  @Override protected void oNext(String resulte, String method) {
    BaseResult<Curriculum> curricul =
        JSONObject.parseObject(resulte, new TypeReference<BaseResult<Curriculum>>() {
        });
    if (curricul.isCode()) {
      curriculumHourList.addAll(curricul.getResult().getCurriculum_hour_list());
      curriculumHourAdapter.updateListView(curriculumHourList);
      curriculumData = curricul.getResult().getCurriculum_data();
      setView();
    }
  }

  @Override protected void oError() {

  }

  /**
   * 设置view
   */
  private void setView() {
    if (curriculumData == null) {
      return;
    }
    ImgUitl.setImg(curriculumData.getCurriculum_top_img(), mTopImg);
    mTvNum.setText("(" + curriculumHourList.size() + ")");
    //未登录
    //curriculum_charge_state 0=免费，1=vip,2=收费
    if (!userInfo.isLogin()) {
      mRlMember.setVisibility(View.VISIBLE);
      switch (curriculumData.getCurriculum_charge_state()) {
        case 0:
          mRlBt.setVisibility(View.GONE);
          break;
        case 1:
          mBtVip.setText("开通会员");
          mRlBt.setVisibility(View.VISIBLE);
          break;
        case 2:
          mBtVip.setText("购买课程");
          mRlBt.setVisibility(View.VISIBLE);
          break;
      }
      return;
    }

    //登录之后
    //永久vip会员
    if (userInfo.getUser_vip_state() == 2) {
      mRlMember.setVisibility(View.GONE);
      mRlBt.setVisibility(View.GONE);
      return;
    }

    //curriculum_charge_state 0=免费，1=vip,2=收费
    if (curriculumData.getCurriculum_charge_state() == 0) {
      mRlBt.setVisibility(View.GONE);
      //0=普通会员,1=vip会员,2=永久vip会员
      if (userInfo.getUser_vip_state() == 0) {
        mRlMember.setVisibility(View.VISIBLE);
      } else {
        mRlMember.setVisibility(View.GONE);
      }
    } else if (curriculumData.getCurriculum_charge_state() == 1) {
      mRlMember.setVisibility(View.GONE);
      if (userInfo.getUser_vip_state() == 0) {
        mRlBt.setVisibility(View.VISIBLE);
        mBtVip.setText("开通会员");
      } else {
        mRlBt.setVisibility(View.GONE);
      }
    } else if (curriculumData.getCurriculum_charge_state() == 2) {
      if (curriculumData.getOrder_state()==0){
        mRlBt.setVisibility(View.VISIBLE);
        mBtVip.setText("购买课程");
      }else {
        mRlBt.setVisibility(View.GONE);
      }
      if (userInfo.getUser_vip_state() == 0) {
        mRlMember.setVisibility(View.VISIBLE);
      } else {
        mRlMember.setVisibility(View.GONE);
      }

    }
  }

  @OnClick({
      R.id.activity_training_catalog_bt_member, R.id.activity_training_catalog_bt_add,
      R.id.activity_training_catalog_bt_vip
  }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.activity_training_catalog_bt_member://开通会员
        if (userInfo.isLogin()) {
          startActivity(new Intent(TrainingCatalogActivity.this, RechargeActivity.class));
        } else {
          startActivity(new Intent(TrainingCatalogActivity.this, LoginActivity.class));
        }
        break;
      case R.id.activity_training_catalog_bt_add://添加到
        if (userInfo.isLogin()) {
          startActivity(
              new Intent(TrainingCatalogActivity.this, TrainingCatalogSecActivity.class).putExtra(
                  "id", curriculum_id));
        } else {
          startActivity(new Intent(TrainingCatalogActivity.this, LoginActivity.class));
        }
        break;
      case R.id.activity_training_catalog_bt_vip://开通VIP(购买课程)
        if (userInfo.isLogin()) {
          if (curriculumData.getCurriculum_charge_state() == 1) {
            //开通VIP
            vipsDialog.show();
          } else if (curriculumData.getCurriculum_charge_state() == 2) {
            //购买课程
            startActivity(
                new Intent(TrainingCatalogActivity.this, ConfirmPurchaseActivity.class).putExtra(
                    "id", curriculum_id).putExtra("type", 3));
          }
        } else {
          startActivity(new Intent(TrainingCatalogActivity.this, LoginActivity.class));
        }
        break;
    }
  }
}
